package cn.abcsys.cloud.devops.runtime.core.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.common.Constant;
import cn.abcsys.cloud.devops.runtime.common.HttpClient;
import cn.abcsys.cloud.devops.runtime.common.Type;
import cn.abcsys.cloud.devops.runtime.core.HostCore;
/**
 * 主机核心操作
 * @author mayunhao
 *
 */
@Component
public class HostCoreImpl implements HostCore {
	@Autowired
	private HttpClient httpClient;
	public Result getHostSystemInfo(String hostName) {
		String url = assembleHostInfoUrl(hostName);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,获取["+hostName+"]操作系统数据失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			return new Result(true,responseObject , "success");
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result isKube(String hostName) {
		String url = assembleIsKubeUrl(hostName);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,获取["+hostName+"] kubenetes系统数据失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
		
	}
	@Override
	public Result startKube(String hostIp, String type,Map<String, String> params) {
		String url = null;
		if(type.equals(Type.HOST.MASTER.name())){
			url = assembleStartMasterUrl(hostIp);
		}else if(type.equals(Type.HOST.NODE.name())){
			url = assembleStartNodeUrl(hostIp);
		}
		String response = httpClient.get(params, url);
		if(null==response){
			return new Result(false, "返回结果为空,启动["+hostIp+"] kubenetes失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result stopKube(String hostIp, String type) {
		String url = null;
		if(type.equals(Type.HOST.MASTER.name())){
			url = assembleStopMasterUrl(hostIp);
		}else if(type.equals(Type.HOST.NODE.name())){
			url = assembleStopNodeUrl(hostIp);
		}
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,停止["+hostIp+"] kubenetes失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result addDns(String nodeHostIp, String masterIp, String masterDnsName) {
		String url = assembleAddDnsUrl(nodeHostIp, masterIp, masterDnsName);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,修改["+nodeHostIp+"] kubenetes域名失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result removeDns(String nodeHostIp, String masterDnsName) {
		String url = assembleRemoveDnsUrl(nodeHostIp,masterDnsName);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,删除["+nodeHostIp+"] kubenetes域名失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result startDistribution(String registryHostIp,Map<String, String> params) {
		String url = assembleStartDistribution(registryHostIp);
		String response = httpClient.get(params, url);
		if(null==response){
			return new Result(false, "返回结果为空,启动["+registryHostIp+"]仓库失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result stopDistribution(String registryHostIp) {
		String url = assembleStopDistribution(registryHostIp);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,停止["+registryHostIp+"]仓库失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result isDistribution(String registryHostIp) {
		String url = assembleIsDistribution(registryHostIp);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,获取["+registryHostIp+"]仓库系统信息失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	/*
	 * 主机操作系统信息
	 */
	private String assembleHostInfoUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/sysinfo";
	}
	/*
	 * k8s master start 
	 */
	private String assembleStartMasterUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/startMaster";
	}
	/*
	 * k8s node start 
	 */
	private String assembleStartNodeUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/startNode";
	}
	/*
	 * k8s master stop 
	 */
	private String assembleStopMasterUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/stopMaster";
	}
	/*
	 * k8s node start 
	 */
	private String assembleStopNodeUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/stopNode";
	}
	/*
	 * 集群域名添加到节点主机hosts文件
	 */
	private String assembleAddDnsUrl(String nodeHostName,String masterIp,String masterDnsName) {
		return "http://" + nodeHostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/newDomain?"+masterIp+"="+masterDnsName;
	}
	/*
	 * 集群域名从节点主机hosts文件移除
	 */
	private String assembleRemoveDnsUrl(String nodeHostName,String masterDnsName) {
		return "http://" + nodeHostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/rmDomain?name="+masterDnsName;
	}
	/*
	 * 是否是k8s主机
	 */
	private String assembleIsKubeUrl(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/isKubernetes";
	}
	/*
	 * 是否是仓库主机
	 */
	private String assembleIsDistribution(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/isDistribution";
	}
	private String assembleStartDistribution(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/startDistribution";
	}
	private String assembleStopDistribution(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/stopDistribution";
	}
	private String assembleDistributionIsRunning(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/isRunning";
	}
	private String assembleMasterRunning(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/masterRunning";
	}
	private String assembleNodeRunning(String hostName) {
		return "http://" + hostName + ":"+Constant.ABCSYS_SERVICE_PORT+"/abcsys/nodeRunning";
	}
	@Override
	public Result isRunning(String registryHostIp) {
		String url = assembleDistributionIsRunning(registryHostIp);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,仓库主机["+registryHostIp+"]检查失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result nodeRunning(String hostIp) {
		String url = assembleNodeRunning(hostIp);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,主机["+hostIp+"] kubernetes 进程检查失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
	@Override
	public Result masterRunning(String hostIp) {
		String url = assembleMasterRunning(hostIp);
		String response = httpClient.get(null, url);
		if(null==response){
			return new Result(false, "返回结果为空,主机["+hostIp+"] kubernetes 进程检查失败");
		}
		try{
			JSONObject responseObject = JSONObject.parseObject(response);
			if(responseObject.getIntValue("status")==Constant.STATUS_200||
					Constant.STATUS_SUCCESSFUL.equals(responseObject.getString("status"))){
				return new Result(true,responseObject , "success");
			}else{
				return new Result(false,response);
			}
		}catch(Exception e){
			return new Result(false, "返回结果异常：["+response+"]");
		}
	}
}
